//
//  GoSetting.m
//  lvfj
//
//  Created by lvfeijun on 2022/8/29.
//  Copyright © 2022 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//

#import "GoSetting.h"

@implementation GoSetting

/**
 显示请求授权弹窗

 @param title 标题
 @param message 描述
 */
- (void)goSettingAlert:(NSString *)title message:(NSString *)message
{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //仅仅是设置，如果跳转进行授权后，会重新启动app，无法进入当前页面，十分不友好。
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (self.promissionBlock) {
                self.promissionBlock();
            }
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //跳转到手机设置页面
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            
            if (self.promissionBlock) {
                self.promissionBlock();
            }
        }]];
    
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (self.navigationController) {
                [self.navigationController presentViewController:alert animated:YES completion:nil];
            }
            else {
                [self presentViewController:alert animated:YES completion:nil];
            }
        });
    });
}

///相册授权
- (void)showPhotoPicker:(NSInteger)maxCount selectedAssets:(NSArray *)assets completeSelectedImages:(selectedImages)selectedImages {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        NSString *appVersion = [NSString stringWithFormat:@"%@_%@",[bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"],[bundle objectForInfoDictionaryKey:@"CFBundleVersion"]];
        NSString *kHQWXCameraVersion = @"kHQWXCameraVersion";
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        NSString *version = [userDefault valueForKey:kHQWXCameraVersion];
        if ([PHPhotoLibrary authorizationStatus] == 0  ||
            ![version isKindOfClass:NSString.class] ||
            ![version isEqualToString:appVersion]) {
            //弹出系统请求权限授权弹窗，只会弹一次。
            @weakify(self);
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                    @strongify(self);
                    [self showPhotoPicker:maxCount selectedAssets:assets completeSelectedImages:selectedImages];
                    [userDefault setValue:appVersion forKey:kHQWXCameraVersion];
                    [userDefault synchronize];
                }];
            });
            
            return;
        }
        
        if (![TZImageManager manager].authorizationStatusAuthorized) {
            //没有访问相册权限
            NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
            NSString *message = [NSString stringWithFormat:@"请在iPhone的 \"设置-%@-相册\" 中允许访问相册", appName];
            
            //@"请在iPhone的""设置-环球网校-相册""中允许访问相册"
            [self showNoPromissionAlert:@"无法访问相册" message:message];
            return;
        }

        QA_ImagePickerVC *imagePickerVC = [[QA_ImagePickerVC alloc] initWithMaxImagesCount:maxCount delegate:nil];
        imagePickerVC.allowPickingVideo = NO;
        imagePickerVC.allowPickingOriginalPhoto = NO;
        imagePickerVC.allowTakePicture = NO;
        imagePickerVC.showSelectBtn = YES;
        imagePickerVC.showSelectedIndex = (maxCount > 1 || maxCount == 0);
        imagePickerVC.selectedAssets = [assets mutableCopy];
        imagePickerVC.modalPresentationStyle = UIModalPresentationFullScreen;

        [imagePickerVC setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            if (selectedImages) {
                selectedImages(photos, assets, isSelectOriginalPhoto);
            }
        }];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self presentViewController:imagePickerVC animated:YES completion:nil];
        });
    });
}

@end
